import {Component, Input, OnInit} from '@angular/core';
import { ApiService } from '@core/services/http/api.service';
import { Events } from '@core/services/events/Events';
import { ActivatedRoute } from '@angular/router';
import { GridOptions } from '@ag-grid-community/core';
import { GridConfigService } from '@core/config/grid-config.service';
import {ApesMessageService} from "@shared/apes/message";

@Component({
  selector: 'service-pro',
  templateUrl: './service-pro.component.html',
  styleUrls: ['./service-pro.component.scss']
})
export class ServiceProComponent implements OnInit {
  @Input()
  set parmes(value: any) {
    this._parmes = value;
  }
  get parmes() {
    return this._parmes;
  }

  _parmes: any;

  /** Grid 相关 **/
  rowData: any[] = [];
  resultData: any;
  gridOptions: GridOptions;
  pinnedBottomRowData: any[] = [];
  columnDefs: any[] = [];
  domLayout: any;
  topList:any [];                // 头部信息
  titleData = {};
  conList:any[];
  allList:any = {};
  query: any;

  constructor(private routeInfo: ActivatedRoute,
              private event:Events, private gridConfigService: GridConfigService,
              private apiService:ApiService, private msg: ApesMessageService) {

    this.columnDefs = [
      { 'headerName': '同意服务标记', field: 'TYZXBJ', width: 120 },
      { 'headerName': '待跟踪标记', field: 'DGZBJ', width: 100 },
      { 'headerName': '必修标记', field: 'BXBJ', width: 100 },
      { 'headerName': '配件名称', field: 'PJ', width: 200 },
      { 'headerName': '商品名称', field: 'SP', width: 200 },
      { 'headerName': '销售数量', field: 'XSSL', width: 100 },
      { 'headerName': '销售单价', field: 'XSDJ', width: 100 },
      { 'headerName': '优惠单价', field: 'YHDJ', width: 100 },
      { 'headerName': '销售金额', field: 'XSJE', width: 100 },
      { 'headerName': '摘要', field: 'ZY', width: 85 },
      { 'headerName': '采购意见', field: 'CGYJ', width: 100 },
      { 'headerName': '要货数量', field: 'YHSL', width: 100 },
      { 'headerName': '施工人员', field: 'SGRY', width: 100 },
      { 'headerName': '内协门店', field: 'NXMDMC', width: 110 },
      { 'headerName': '外协标记', field: 'SFWXBJ', width: 100 },
      { 'headerName': '终止标记', field: 'ZZBJ', width: 100  }
    ];

    // 初始化 grid
    this.gridOptions = <GridOptions>{
      toolPanelSuppressSideButtons: true,
      suppressExcelExport: true,
      suppressCsvExport: true,
      suppressSizeToFit: true,
      showToolPanel: false,
      pagination: true,
      rowHeight: 35,
    };
    this.gridOptions.localeText = gridConfigService.getLocaleText();
    this.domLayout = 'autoHeight';
  }

  ngOnInit() {
    this.conList = this.setHeadData();
  }

  ngAfterViewInit(){
    let jjcdh = this.parmes.JJCDH;
    let cphm = this.parmes.CPHM;
    let mdid = this.parmes.MDID;
    this.getData(jjcdh,cphm, mdid);
  }

  // 双击事件
  getClick(value ,label) {
    if(label !== '服务单号'){
      this.query = value;
      this.getFWFAHData('', value);
    }else{
      this.query = value;
      this.getFWFAHData(value, '');
    }
  }

  // 脑袋数据
  setHeadData() {
    return [
      {
        titleData: [
          {lable: '服务门店', feild: 'FWMDMC'},
          {lable: '单据状态', feild: 'DJZT'},
          {lable: '来源单据号码', feild: 'LYDJHM'},
          {lable: '客户要求及描述', feild: 'KHMS'},
          {lable: '故障现象', feild: 'FWGZXX'},
          {lable: '诊断结果', feild: 'ZDJG'},
          {lable: '特别提示', feild: 'AQTS'},
          {lable: '里程表读数', feild: 'LCBDS'},
          {lable: '销售金额', feild: 'XSJE'}

        ]
      }
    ]
  }

  // 获取服务单号
  getData(jjcdh?: any, cphm? :any, mdid?: any) {
    let data = {
      'interfaceMethod': 'ipmCLZKForAppService.getFWFAHEAD', 'pdata': {'JJCDH':jjcdh, 'CPHM': cphm, 'MDID': mdid},
    };
    this.apiService.post('report.interface', { 'body': JSON.stringify(data) }).subscribe(
      (resultData) => {
        const result = JSON.parse(resultData);
        if (result.success) {
          if (result.obj.length !== 0) {
            let data = JSON.parse(result.obj);
            this.topList = data.mainArray;
            this.query = this.topList[0].FWDH;
            this.getFWFAHData(this.query, '');
          }
        } else {
          this.msg.info(result.msg);
        }
      });
  }

  // 获取具体服务方案
  getFWFAHData( fwdh, fwfah) {
    let data = {
      'interfaceMethod': 'ipmCLZKForAppService.getFWFAHEAD', 'pdata': { 'FWDH': fwdh, 'FWFAH':fwfah },
    };
    this.apiService.post('report.interface', { 'body': JSON.stringify(data) }).subscribe(
      (resultData) => {
        const result = JSON.parse(resultData);
        if (result.success) {
          if (result.obj.length !== 0) {
            let data = JSON.parse(result.obj);
            for (let i = 0 ,len = data.faJson.array.length; i < len ; i++){
              if(data.faJson.array[i]['TYZXBJ'] === 1){
                data.faJson.array[i]['TYZXBJ'] = '√';
              }
            }
            this.allList = data.mainArray[0];
            this.rowData = data.faJson.array;
          }
        } else {
          this.msg.info(result.msg);
        }
      });
  }

}
